package com.whnk.web.controller.evo;

import java.io.IOException;
import java.util.List;

import net.sf.jasperreports.engine.JRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whnk.common.annotation.Log;
import com.whnk.common.base.AjaxResult;
import com.whnk.common.enums.BusinessType;
import com.whnk.evo.domain.VoucherInfo;
import com.whnk.evo.service.IVoucherInfoService;
import com.whnk.framework.web.base.BaseController;
import com.whnk.framework.web.page.TableDataInfo;

/**
 * 电子凭证 信息操作处理
 *
 * @author whwhnk
 * @date 2019-09-03
 */
@Controller
@RequestMapping("/evo/voucherInfo")
public class VoucherInfoController extends BaseController
{
    private String prefix = "evo/voucherInfo";

	@Autowired
	private IVoucherInfoService voucherInfoService;

	@RequiresPermissions("evo:voucherInfo:view")
	@GetMapping()
	public String VoucherInfo()
	{
	    return prefix + "/voucherInfo";
	}

	/**
	 * 查询电子凭证列表
	 */
	@RequiresPermissions("evo:voucherInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(VoucherInfo voucherInfo)
	{
		startPage();
        List<VoucherInfo> list = voucherInfoService.selectVoucherInfoList(voucherInfo);
		return getDataTable(list);
	}

	/**
	 * 新增电子凭证
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 新增保存电子凭证
	 */
	@RequiresPermissions("evo:voucherInfo:add")
	@Log(title = "电子凭证", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(VoucherInfo voucherInfo)
	{
		return toAjax(voucherInfoService.insertVoucherInfo(voucherInfo));
	}

	/**
	 * 修改电子凭证
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		VoucherInfo VoucherInfo = voucherInfoService.selectVoucherInfoById(id);
		mmap.put("voucherInfo", VoucherInfo);
	    return prefix + "/edit";
	}

	/**
	 * 修改保存电子凭证
	 */
	@RequiresPermissions("evo:voucherInfo:edit")
	@Log(title = "电子凭证", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(VoucherInfo voucherInfo)
	{
		return toAjax(voucherInfoService.updateVoucherInfo(voucherInfo));
	}

	/**
	 * 删除电子凭证
	 */
	@RequiresPermissions("evo:voucherInfo:remove")
	@Log(title = "电子凭证", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		return toAjax(voucherInfoService.deleteVoucherInfoByIds(ids));
	}

	/**
	 * 查看详细信息
	 *
	 * @param id
	 * @param mmap
	 * @return
	 */
	@RequiresPermissions("evo:voucherInfo:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap)
    {
		VoucherInfo VoucherInfo = voucherInfoService.selectVoucherInfoById(id);
		mmap.put("voucherInfo", VoucherInfo);
        return prefix + "/detail";
    }

	/**
	 * TODO 生成电子凭证PDF
	 *
	 * @param id
	 * @return
	 */
	@PostMapping("/expEvoById")
	@ResponseBody
    public AjaxResult expEvoById(String id) throws IOException, JRException {
		// 根据ID查询模板和数据，生成PDF文件，返回前端页面
		String result = voucherInfoService.expEvoById(Integer.valueOf(id));

		return AjaxResult.success(result);
    }
}
